package com.kun.demo04_synchronize.solution;

import org.junit.Test;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class NotSafeCase3_list {

//    List<String> list = new java.util.ArrayList<>();

//    class TestList implements Runnable {
//
//        @Override
//        public void run() {
//            synchronized (list) {
//                list.add(Thread.currentThread().currentThread().getName());
//            }
//        }
//    }


    @Test
    public void test() throws InterruptedException {
//        TestList testList = new TestList();
        CopyOnWriteArrayList<String> list1 = new CopyOnWriteArrayList<>();

        for (int i = 0; i < 10000; i++) {
            new Thread(()->{
                list1.add(Thread.currentThread().getName());
            }).start();
//            new Thread(testList, "thread-" + i).start();
        }
        Thread.sleep(2000);
        System.out.println("list预想长度："+10000);
        System.out.println("list实际长度："+list1.size());

    }
}
